function [err] = depfits(varargin)
% objective function for optimization
        plota = 0;
clist = {'r', 'g', 'b', 'c', 'k'};
mlist = {'s', 'o', '^', 'd', 'x', '+'};
if(nargin == 0)
    % synapse parameters:
% (Dittman and Regehr model)
table.F = 0.4; % release probability (constant; no facilitiation)
table.k0 = 1/1.75; % /s, baseline recovery rate from depletion (slow rate)
table.kmax = 1/0.025; % /s, maximal recovery rate from depletion (fast rate)

table.td = 0.05; %  time constant for calcium-dependent recovery
table.kd =  0.7; % affinity of fast recovery proces for calcium sensor

table.ts = 0.015; % decay time constant of glutatme clearance
table.ks = 1000; % affinity of receptor desensitization for glutatmate
% The large value means no desense occurs (otherwise, ks should be about
% 0.6)

table.kf = 0.6; % affinity of facilitiation process
table.tf = 0.01; % make facilitation VERY slow

table.dD = 0.02; % sets Ca that drives recovery(Ca influx per AP)
% 0.02 yields rate-dep recovery in 100-300 Hz
table.dF = 0.02; % sets Ca that drives facilitation

table.glu = 0.3;
else
    p = varargin{1};
    % note: order is important!!!!!
    table.F = p(1);
    table.k0 = p(2);
    table.kmax = p(3);
    table.td = p(4);
    table.kd = p(5);
    table.ts = p(6);
    table.ks = p(7);
    table.kf = p(8);
    table.tf = p(9);
    table.dD = p(10);
    table.dF = p(11);
    table.glu = p(12);
    
end;


% data: Y. Wang, CBA adult mouse end bulb EPSC/recovery at different
% frequencies. Normalized to first response amplitude.
freq = [100, 200, 300];
tb{1}=[5:10:145];
ib{1}= [1, 0.986, 0.929, 0.885, 0.850, 0.817, 0.766, 0.749, 0.734, 0.713, ...
    0.718, 0.708, 0.696, 0.705, 0.709];
tb{2}=[5:5:100];
ib{2}=[1, 0.973, 0.901, 0.832, 0.773, 0.718, 0.664, 0.641, 0.609, 0.596, ...
    0.526, 0.553, 0.532, 0.513, 0.495, 0.489, 0.490, 0.497, 0.485, 0.485];
tb{3} = [5:3.33:68.3];
ib{3}=[1,0.971, 0.880, 0.726, 0.631, 0.563, 0.504, 0.483, 0.465, 0.430, ...
    0.423, 0.400, 0.377, 0.358, 0.358, 0.352, 0.328, 0.329, 0.317, 0.319];
% recovery
trb{1} = [6, 30, 56, 88, 123, 164, 212, 266, 329, 401, 3000];
rb{1} = [0.717, 0.701, 0.716, 0.678, 0.700, 0.728, 0.703, 0.711, 0.732, ...
    0.716, 0.989];
%trb{2} = [16, 38, 74, 95, 132, 175, 228, 290, 363, 451, 5000];
trb{2} = [6, 30, 56, 88, 123, 164, 212, 266, 329, 401, 3000];
rb{2} = [ 0.616, 0.724, 0.732, 0.724, 0.765, 0.760, 0.790, 0.793, 0.780, ...
    0.789, 1];
%trb{3} = [16, 38, 74, 95, 132, 175, 228, 290, 363, 451, 5000];
trb{3} = [6, 30, 56, 88, 123, 164, 212, 266, 329, 401, 3000];
rb{3} = [0.378, 0.629, 0.699, 0.721, 0.721, 0.843, 0.790, 0.799, 0.793, ...
    0.832, 1.0];
for i = 1:length(tb)
    tb{i} = 0.001*tb{i};
end;
for i = 1:length(trb)
    trb{i} = 0.001*trb{i};
end;

newfigure('depfits', 'Depr. Fits');
if(plota)
    for i = 1:length(tb)
        plot(tb{i}, ib{i}, 'ro-');
        hold on;
    end;
    subplot(2,1,2)
    for i = 1:length(trb)
        plot(trb{i}, rb{i}, 'kx-');
        hold on;
    end;
end;

err = 0;
for i = 1:length(tb)
    ntb{i} = [tb{i} max(tb{i})+trb{i}];
    nr{i} = [ib{i} rb{i}];
    nr{i} = nr{i}/nr{i}(1);
    plot(ntb{i}, nr{i}, [clist{i} mlist{i}], 'markersize', 2.5, 'markerfacecolor', clist{i});
    hold on;
    set(gca, 'Xlim', [0 0.5]);
    set(gca, 'Ylim', [0 1.2]);

end;

[xo, yo] = XuF(2, table, tb, trb);
for i = 1:length(xo)
    hold on
    plot(xo{i}, yo{i}, [clist{i} '+-'], 'markersize', 1.75);
end;

err = 0;
for i = 1:length(xo)
    err = err + sum((yo{i}' - nr{i}).^2);
end;




















